﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using ScaleDataInterpreter;
using weighing;
using clf.weighing.entity;

namespace weighing
{
    public class ScaleVo : BaseVo
    {
        public System.Threading.Timer Timer { get; set; }
        public readonly Scale mScale;
        public ScaleVo(Scale scale)
        {
            mScale = scale;
        }

        public IScaleDataInterpreter Interpreter { get; set; }
        public System.IO.Ports.SerialPort SerialPort { get; set; }

        private double tonner;
        private bool isSelected;
        public string info;
        public int status;

        public long Id
        {
            get { return mScale.id; }
            set
            {
                mScale.id = value;
                OnPropertyChanged(this, nameof(Id));
            }
        }
        public int? BaudRate
        {
            get { return mScale.baudRate; }
            set
            {
                mScale.baudRate = value;
                OnPropertyChanged(this, nameof(BaudRate));
            }
        }
        public ScaleBrandType BrandType
        {
            get { return (ScaleBrandType)(mScale.brandType ?? (int)ScaleBrandType.YH); }
            set
            {
                mScale.brandType = (int)value;
                OnPropertyChanged(this, nameof(BrandType));
            }
        }
        public String Com
        {
            get { return mScale.com; }
            set
            {
                mScale.com = value;
                OnPropertyChanged(this, nameof(Com));
            }
        }
        public String Name
        {
            get { return mScale.name; }
            set
            {
                mScale.name = value;
                OnPropertyChanged(this, nameof(Name));
            }
        }
        public String Info
        {
            get { return info; }
            set
            {
                info = value;
                OnPropertyChanged(this, nameof(Info));
            }
        }
        public DateTime? AddTime
        {
            get
            {
                return mScale.addTime;
            }
            set
            {
                mScale.addTime = value;
                OnPropertyChanged(this, nameof(AddTime));
            }
        }

        public bool IsSelected
        {
            get { return isSelected; }
            set
            {
                isSelected = value;
                OnPropertyChanged(this, nameof(IsSelected));
            }
        }

        public int Status
        {
            get { return status; }
            set
            {
                status = value;
                OnPropertyChanged(this, nameof(Status));
            }
        }
        public double Tonner
        {
            get { return tonner; }
            set
            {
                tonner = value;
                OnPropertyChanged(this, nameof(Tonner));
                OnPropertyChanged(this, nameof(TonnerText));
            }
        }

        public Int32? DefaultType
        {
            get { return mScale.defaultType; }
            set
            {
                mScale.defaultType = value;
                OnPropertyChanged(this, nameof(DefaultType));
            }
        }

        public void DisposeTimer()
        {
            Timer?.Dispose();
        }

        public void DisposeSerialPort()
        {
            if (SerialPort != null)
            {
                SerialPort.Close();
                if (SerialPort.IsOpen == true)
                {
                    SerialPort.BaseStream?.Dispose();
                    SerialPort.Close();                   
                }               
                SerialPort.Dispose();
            }
        }

        public String Brand
        {
            get { return mScale.brand; }
            set
            {
                mScale.brand = value;
                OnPropertyChanged(this, nameof(Brand));
            }
        }

        public String Series
        {
            get
            { return mScale.series; }
            set
            {
                mScale.series = value;
                OnPropertyChanged(this, nameof(Series));
            }
        }
        public String AddUserName
        {
            get
            {
                return mScale.addUserName;
            }
            set
            {
                mScale.addUserName = value;
                OnPropertyChanged(this, nameof(AddUserName));
            }
        }
        /// <summary>
        /// 可空:YES
        /// </summary>
        public Int64? SyncTime
        {
            get
            {
                return mScale.syncTime;
            }
            set
            {
                mScale.syncTime = value;
                OnPropertyChanged(this, nameof(SyncTime));
            }
        }

        public bool IsVirtual
        {
            get { return mScale.isVirtual; }
            set
            {
                mScale.isVirtual = value;
                OnPropertyChanged(this, nameof(IsVirtual));
                OnPropertyChanged(this, nameof(Location));
            }
        }

        public bool IsShare
        {
            get { return mScale.isShare; }
            set
            {
                mScale.isShare = value;
                OnPropertyChanged(this, nameof(IsShare));
                OnPropertyChanged(this, nameof(Location));
            }
        }
        #region TEXT

        public String Location
        {
            get
            {
                if (mScale.isVirtual) return LangHelper.Get(LangKeyNames.Virtual);
                if (mScale.isShare) return LangHelper.Get(LangKeyNames.Shared);
                return "";
            }
        }
        public String TonnerText
        {
            get
            {
                int digits = 2;
                try
                {
                    //防止系统未配制小数位数是出错
                    digits = App.Config.WeighingConfig.Digits;
                }
                catch (Exception)
                {
                }
              return Tonner.ToString($"F{digits}");
            }
        }
        

        #endregion
    }
}
